package com.gxa.community.admin.controller.content;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.community.admin.advice.Log;
import com.gxa.community.admin.service.content.PlatformContentService;
import com.gxa.community.core.pojo.domain.SystemInfo;
import com.gxa.community.core.pojo.dto.content.PagePlatformContentDTO;
import com.gxa.community.core.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/content")
public class PlatformContentController {

    @Autowired
    private PlatformContentService platformContentService;

    @ApiOperation("获取平台所有资料")
    @GetMapping("/getall")
    public R getAll(){
        //@RequestParam("currentPage")Integer currentPage,@RequestParam("pageSize")Integer pagaSize
//        PageHelper.startPage(currentPage,pagaSize);
        List<SystemInfo> list = platformContentService.list();
        if (!CollectionUtils.isEmpty(list)){
//            PageInfo<SystemInfo> pageInfo = new PageInfo<>(list);
//            PagePlatformContentDTO pagePlatformContentDTO = new PagePlatformContentDTO();
//            pagePlatformContentDTO.setTotal(pageInfo.getTotal());
//            pagePlatformContentDTO.setPages(pageInfo.getPages());
//            pagePlatformContentDTO.setCurrentPage(currentPage);
//            pagePlatformContentDTO.setPageSize(pagaSize);
//            pagePlatformContentDTO.setContents(list);
            return R.ok("查询成功！").put("data",list);
        }else {
            return R.error(600,"查询失败，请联系管理员！");
        }
    }

    @ApiOperation("根据id查询单个资料内容")
    @GetMapping("/getsinglecontentbyid/{id}")
    public R getSingleContentById(@PathVariable("id")Integer id){
        SystemInfo systemInfo = platformContentService.getById(id);
        return systemInfo!=null?R.ok("查询成功！").put("data",systemInfo):R.error(600,"查询失败，请联系管理员！！");
    }

    @ApiOperation("修改单个资料内容")
    @PutMapping("/updatesinglecontent")
    @Log(title = "",action = "修改了平台资料内容")
    public R updateSingleContent(@RequestBody SystemInfo systemInfo){
        systemInfo.setUpdateTime(System.currentTimeMillis());
        boolean result = platformContentService.saveOrUpdate(systemInfo);
        return result?R.ok("修改成功！"):R.error(600,"修改失败，请联系管理员！！");
    }
}
